﻿using System;
using System.Diagnostics;
using Lenic.DI.Lifetimes;

namespace Lenic.DI
{
    /// <summary>
    /// DI 容器注册项
    /// </summary>
    /// <typeparam name="TService">委托返回值类型</typeparam>
    [DebuggerStepThrough]
    public class Registration<TService>
    {
        #region Business Properties

        /// <summary>
        /// 获取待注册的 ID 类型
        /// </summary>
        internal Type ServiceType { get; private set; }

        /// <summary>
        /// 获取待注册的 ID 名称
        /// </summary>
        internal string Name { get; private set; }

        /// <summary>
        /// 获取待注册的 ID 缓存值
        /// </summary>
        internal ILifetime Value { get; private set; }

        #endregion Business Properties

        #region Entrance

        /// <summary>
        /// 获取一个新建的 <see cref="Registration"/> 类的实例对象。
        /// </summary>
        public static Registration<TService> New(ILifetime lifetime = null)
        {
            if (lifetime == null)
                lifetime = new DefaultLifetime();

            return new Registration<TService>(lifetime);
        }

        /// <summary>
        /// 初始化新建一个 <see cref="Registration"/> 类的实例对象。
        /// </summary>
        /// <param name="lifetime">一个生存周期控制的实例对象。</param>
        private Registration(ILifetime lifetime)
        {
            ServiceType = typeof(TService);
            Value = lifetime;
        }

        #endregion Entrance

        #region Business Methods

        /// <summary>
        /// 命名当前注册项
        /// </summary>
        /// <param name="name">新的注册项名称</param>
        /// <returns>修改后的自身</returns>
        public Registration<TService> Named(string name)
        {
            Name = name;

            return this;
        }

        /// <summary>
        /// 为当前注册项添加获取委托
        /// </summary>
        /// <param name="func">返回值获取委托</param>
        /// <returns>修改后的自身</returns>
        public Registration<TService> Register(Delegate func)
        {
            Value.Function = func;

            return this;
        }

        #endregion Business Methods
    }
}